<?php
/**
 * Created by Engineer CuiLiwu.
 * Project: deal.
 * Date: 2018/5/18-9:41
 * License Hangzhou orce Technology Co., Ltd. Copyright © 2018
 */


namespace App\Http\Controllers\Backend\Article;
use App\ConstDir\ErrorConst;
use App\Http\Controllers\Backend\BaseController;
use App\Repository\Models\ArticleCategory;
use App\Repository\Repositories\Interfaces\SysArticleRepository;
use App\Repository\Repositories\Interfaces\ArticleCategoryRepository;
use Illuminate\Http\Request;


class ArticleController  extends BaseController
{
    protected $article;
    protected $flag;
    protected $articleCate;
    protected $request;
    public function __construct(SysArticleRepository $article,Request $request,ArticleCategoryRepository $articleCate) {
        $this->article=$article;
        $this->request=$request;
        $this->flag   =[];
        $this->articleCate=$articleCate;
    }

    /**
     * 文章列表
     * 分页
     * */
    public function index(){

        $data=$this->article->with('category')->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE, $data, true);
    }

    /**
     * 文章详情
     * @param $id
     * @return \Illuminate\Support\Collection
     */
    public function show($id){
        $data=$this->article->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE, $data);

    }
    /**
     * 文章添加
     *
     * */
    public function store(){
        $data=$this->request->all();
        $Category=ArticleCategory::query()->where('id',$data['fid'])->get()->toArray();
        $data['type_id']=$Category[0]['type_id'];

        $getPid     = $this->articleCate->getPid($data['fid']);
        $data['pid']= $getPid;

        $ret=$this->article->create($data);
        return $this->success(ErrorConst::SUCCESS_CODE, $data);

    }
    /**
     * 文章修改
     *
     * */
    public function update($id){
        $data=$this->request->all();
        $Category=ArticleCategory::query()->where('id',$data['fid'])->get()->toArray();
        $data['type_id']=$Category[0]['type_id'];

        $getPid     = $this->articleCate->getPid($data['fid']);
        $data['pid']= $getPid;

        $ret=$this->article->update($data,$id);
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);

    }

    /**
     * 删除文章 无软删除
     *
     * */
    public function delete($id){
        $ret=$this->article->delete($id);
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);
    }
    /**
     * 设置前台展示
     *
     * */
    public function audit($id){
        $data=$this->request->all(['audit']);
        $ret=$this->article->update($data,$id);
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);
    }

    /**
     * 推荐
     */
    public function recommend($id){
        $data=$this->request->all(['recommend']);
        $ret=$this->article->update($data,$id);
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);
    }

    /**
     * 获取文章分类
     */
    public function category(){
        $ret['fid']=ArticleCategory::query()->where('fid','!=',0)->get()->toArray();
        $ret['pid']=ArticleCategory::query()->where('fid',0)->get()->toArray();
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);

    }
    /**
     * 获取文章子分类
     */
    public function categoryfid($id) {
        $data=$this->request->all();
        $ret=ArticleCategory::query()->where('fid',$data['fid'])->get()->toArray();

        return $this->success(ErrorConst::SUCCESS_CODE, $ret);
    }
    /**
     * 获取文章子分类,没有就返回本身
     */
    public function categoryChildren($id) {
        $ret=$this->article->getSysCateList($id);
        $this->dealCate($ret);
        $return = $this->flag;
        return $this->success(ErrorConst::SUCCESS_CODE, $return);
    }
    /**
     * 处理指定数组
     * */
    private function dealCate($return){
        foreach ($return as $key=>$value){
            array_push($this->flag,$value['id']);
            if (isset($value['children'])){
                self::dealCate($value['children']);
            }
        }
        return $return;
    }


}